//
//  RSWindow.m
//  RemoteSay
//
//  Created by Jan Lehnardt on 29/03/2007.
//  Copyright 2007 Jan Lehnardt. All rights reserved.
//

#import "RSWindow.h"


@implementation RSWindow
-(id) initWithContentRect: (NSRect) contentRect 
				styleMask: (unsigned int) styleMask 
				  backing: (NSBackingStoreType) backingType 
					defer: (BOOL) flag {
	
	
	if ((self = [super initWithContentRect:contentRect
								 styleMask: NSBorderlessWindowMask 
								   backing:backingType
		                                   defer: flag])) {
		/* May want to setup some other options, 
		like transparent background or something */


		[self setBackgroundColor: [NSColor clearColor]];
		[self setLevel: NSStatusWindowLevel];
		[self setAlphaValue: 1.0];
		[self setOpaque: NO];
		[self setHasShadow: YES];
	}
	
	return self;
}

-(BOOL) canBecomeKeyWindow
{
	return YES;
}


-(void)mouseDragged:(NSEvent *)theEvent
{
	NSPoint         currentLocation, newOrigin;
	NSRect          newWindowFrame;

/* If we get here and aren't yet in a left mouse event, which can happen if the user began dragging while
	* a contextual menu is showing, start off from the right position by getting our originalMouseLocation.
*/    
	if (!inLeftMouseEvent) {
//Grab the mouse location in global coordinates
		originalMouseLocation = [self convertBaseToScreen:[theEvent locationInWindow]];
		windowFrame = [self frame];
		inLeftMouseEvent = YES;         
	}

	newOrigin = windowFrame.origin;
	newWindowFrame = windowFrame;

//Grab the current mouse location to compare with the location of the mouse when the drag started (stored in mouseDown:)
	currentLocation = [NSEvent mouseLocation];
	newOrigin.x += (currentLocation.x - originalMouseLocation.x);
	newOrigin.y += currentLocation.y - originalMouseLocation.y;

	newWindowFrame.origin = newOrigin;

	[[NSNotificationCenter defaultCenter] postNotificationName:NSWindowWillMoveNotification object:self];
	[self setFrameOrigin:newWindowFrame.origin];
	[[NSNotificationCenter defaultCenter] postNotificationName:NSWindowDidMoveNotification object:self];         
}

/*!
* @brief Mouse down
	*
	* We start tracking the a drag operation here when the user first clicks the mouse without command presed
	* to establish the initial location.
*/
	- (void)mouseDown:(NSEvent *)theEvent
{

//grab the mouse location in global coordinates
	originalMouseLocation = [self convertBaseToScreen:[theEvent locationInWindow]];
	windowFrame = [self frame];
	inLeftMouseEvent = YES;
}

/*!
* @brief Mouse up
*/
	- (void)mouseUp:(NSEvent *)theEvent
{
	inLeftMouseEvent = NO;
}



@end
